package cate.game.play.skill.passive.awaken;

import cate.common.table.d.GDAttr;
import cate.common.util.XT;
import cate.game.attr.FightAttr;
import cate.game.play.base.TargetFilterParam;
import cate.game.play.base.TargetSelector;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.MoveCtx;
import cate.game.play.skill.effect.SkillEffect;
import cate.game.play.skill.passive.PassiveHandler;

/**
 * 效果1：每次进入觉醒状态净化自身2个负面状态；觉醒状态下每次出手提升自身7%灵能攻击(不可驱散)，每回合至多3层，最多15层
 * 效果2：【觉醒·摧毁】额外提升20%灵能强化，且每次出手时有40%概率增加自身25%觉醒值，每回合至多2次效果3：
 * 效果3：每次进入觉醒状态时随机选择1名单位进行决斗，对该目标造成的伤害提升15%，受其他目标造成的伤害减少15%。
 *       若决斗目标在决斗期间死亡，发起者会获得其10%基础攻击，数值不低于自身5%基础攻击，持续到战斗结束
 */
public class 歼灭皇冠PH extends PassiveHandler {

	private int 驱散数量;
	private int 出手buff;
	private int 回合添加;

	private int 提升概率;
	private int 提升值;
	private int 回合提升;

	private int 决斗buff;

	private double 加伤;
	private double 减伤;
	private double 攻击系数;
	private double 保底系数;


	//驱散数量=2&出手buff=？&回合添加=3
	//&提升概率=4000&提升值=25&回合提升=2
	//决斗buff=？？&加伤=1500&减伤=1500&攻击系数=1000&保底系数=500
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		驱散数量 = args.getInt("驱散数量", 0);
		出手buff = args.getInt("出手buff", 0);
		回合添加 = args.getInt("回合添加", 0);

		提升概率 = args.getInt("提升概率", 0);
		提升值 = args.getInt("提升值", 0);
		回合提升 = args.getInt("回合提升", 0);


		决斗buff = args.getInt("决斗buff", 0);
		加伤 = args.getDouble("加伤", 0d);
		减伤 = args.getDouble("减伤", 0d);
		攻击系数 = args.getWDouble("攻击系数", 0d);
		保底系数 = args.getWDouble("保底系数", 0d);
	}

	@Override
	public void onAwaken(ActionCtx action, boolean start) {
		if (start) {
			if (驱散数量 > 0) {
				skill.owner.buff.tryDispelBad(action, 驱散数量);
			}
			if (决斗buff > 0) {
				Fighter one = TargetSelector.findOne(skill.owner, action.getTeamVs(), new TargetFilterParam());
				if (one != null) {
					battlePid = one.getPid();
					one.buff.tryAddByTid(action, skill.owner, 决斗buff);
				}
			}
		}
	}

	@Override
	public void onRoundBegin(ActionCtx action) {
		roundAddBuff = 0;
		roundAddEnergy = 0;
	}

	@Override
	public void onOwnerMoveDone(MoveCtx move, TimeActionCtx action) {
		if (!skill.owner.status.isAwaken()) {
			return;
		}
		handleAddBuff(action);
		handleAddEnergy(action);
	}

	private int roundAddBuff;

	private void handleAddBuff(ActionCtx action){
		if (roundAddBuff >= 回合添加) {
			return;
		}
		roundAddBuff++;
		skill.owner.buff.tryAddByTid(action, 出手buff);
	}

	private int roundAddEnergy;

	private void handleAddEnergy(ActionCtx action) {
		if (roundAddEnergy >= 回合提升) {
			return;
		}
		if (!XT.chance(提升概率)) {
			return;
		}
		roundAddEnergy++;
		skill.owner.skill.addAwakenEnergy(action, 提升值);
	}

	private int battlePid;

	@Override
	public void beforeSkillSettleAsActor(SkillActionCtx action, SkillEffect effect, Fighter target) {
		if (battlePid <= 0) {
			return;
		}
		if (action.getSkill().cfg.isHurtSkill() && target.getPid() == battlePid) {
			effect.getTargetCtx(target).addActorAttr(new FightAttr(GDAttr.DAMAGE_ADD, 加伤));
		}
	}

	@Override
	public void beforeSkillSettleAsTarget(SkillActionCtx action, SkillEffect effect) {
		if (battlePid <= 0) {
			return;
		}
		if (action.getSkill().cfg.isHurtSkill() && action.getActor().getPid() != battlePid) {
			effect.getTargetCtx(skill.owner).addTargetAttr(new FightAttr(GDAttr.HURT_IGN, 减伤));
		}
	}

	@Override
	public void onOtherDead(ActionCtx action, Fighter killer, Fighter deader) {
		if (deader.getPid() != battlePid) {
			return;
		}
		battlePid = 0;
		double value = deader.attr.total().att() * 攻击系数;
		double min = skill.owner.attr.total().att() * 保底系数;
		value = Math.max(value, min);
		skill.owner.attr.addAttr(new FightAttr(GDAttr.ATT, value));
	}
}
